/***************************************************************************************************************
 *   Author : Manu Joseph
 *   Date : 05/05/2010
 *   Description : A tool that easily allow users to share folders and set permissions
 *   File Name : WaitForm.cs
 *   Purpose : Form indicating progress while a background operation is going on
****************************************************************************************************************/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace EasyShare
{
    public partial class WaitForm : Form
    {
        private delegate void StepProgressBarDelegate(int intProgressValue);
        private delegate void CloseWaitFormDelegate();

        #region Private Instance Variables

        private IAsyncResult m_WaitHandle = null;
        private string m_strCaption = null;
        private string m_strMessage = null;

        #endregion

        public WaitForm(IAsyncResult WaitHandle, string Caption, string Message)
        {
            InitializeComponent();

            this.m_WaitHandle = WaitHandle;
            this.m_strCaption = Caption;
            this.m_strMessage = Message;
        }

        private void TMUCommunicationWaitForm_Load(object sender, EventArgs e)
        {
            this.Text = m_strCaption;
            this.ProgressMessageLabel.Text = m_strMessage + " Please wait";

            System.Threading.Thread objWaitThread = new System.Threading.Thread(new System.Threading.ThreadStart(WaitForHandle));
            objWaitThread.Start();
        }

        private void WaitForHandle()
        {
            while (m_WaitHandle.IsCompleted == false)
            {
                int intProgressValue = (WaitMarqueeProgressBar.Value + 2) % (WaitMarqueeProgressBar.Maximum - WaitMarqueeProgressBar.Minimum);
                SetProgressValue(intProgressValue);
                System.Threading.Thread.Sleep(100);
            }
            CloseWaitForm();
        }

        private void SetProgressValue(int intValue)
        {
            if (this.WaitMarqueeProgressBar.InvokeRequired == true)
            {
                StepProgressBarDelegate objStepProgressBar = new StepProgressBarDelegate(SetProgressValue);
                this.WaitMarqueeProgressBar.Invoke(objStepProgressBar, new object[] { intValue });
            }
            else
            {
                this.WaitMarqueeProgressBar.Value = intValue;
            }
        }

        private void CloseWaitForm()
        {
            if (this.InvokeRequired == true)
            {
                CloseWaitFormDelegate objCloseWaitForm = new CloseWaitFormDelegate(CloseWaitForm);
                this.Invoke(objCloseWaitForm);
            }
            else
            {
                this.Close();
            }
        }
    }
}